package com.sojson.util.annotation;

import java.lang.annotation.Annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;

import com.sojson.util.StringUtil;

/**
 * 注解工具类
 * 
 * @author liu
 * @date 2021-07-04
 */
public class AnnoUtil {

    /** 注解占位符类名前缀 */
    public static final String VALUE_CLASS_PRIFIX = "#{T(";
    /** 注解占位符方法名前缀 */
    public static final String VALUE_METHOD_PRIFIX = ").";
    /** 注解占位符参数名前缀 */
    public static final String VALUE_PARAM_PRIFIX = "('";
    /** 注解占位符后缀 */
    public static final String VALUE_PARAM_SUFFIX = "')}";

    /**
     * 获取注解的包路径
     */
    public static String getPackagePath() {
        return null;
    }

    /**
     * 获取方法上的注解
     */
    public static <T extends Annotation> T getAnnoByMethod(JoinPoint point, Class<T> anno) {
        MethodSignature signature = (MethodSignature)point.getSignature();
        return AnnotationUtils.findAnnotation(signature.getMethod(), anno);
    }

    /**
     * 获取类和方法上的注解(方法优先)
     */
    public static <T extends Annotation> T getAnnoByMethodAndClass(JoinPoint point, Class<T> anno) {
        MethodSignature signature = (MethodSignature)point.getSignature();
        T dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), anno);
        if (StringUtil.isNotBlankObject(dataSource)) {
            return dataSource;
        }

        return AnnotationUtils.findAnnotation(signature.getDeclaringType(), anno);
    }

}